package com.lmx.common.util;

import java.util.Properties;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.lmx.common.constants.CommonConstants;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

/**
 * Created by qin on 2016/6/17.
 */
public class SMSUtil {
	private static Log logger = LogFactory.getLog(SMSUtil.class);

	private String url = ""; // 请求服务地址
	private String appkey = "";// 应用appkey
	private String secret = "";// 密钥
	private String signName = "";// 签名
	private String regTemp = "";// 注册模板
	private String resetTemp = ""; // 重置密码模板
	private String callingTemp = ""; // 呼叫模板
	private Properties properties;

	private static class SMSUtilInstance {
		private static SMSUtil SMSUtil = new SMSUtil();
	}

	public static SMSUtil getInstance() {
		return SMSUtilInstance.SMSUtil;
	}

	/**
	 * 初始化方法
	 */
	public SMSUtil() {
		try {
			properties = PropertiesFileUtil.getProperties("application.properties");
			this.url = properties.getProperty("sms.url");
			this.appkey = properties.getProperty("sms.appkey");
			this.secret = properties.getProperty("sms.secret");
			this.signName = properties.getProperty("sms.sign.name");
			this.regTemp = properties.getProperty("sms.temp.reg");
			this.resetTemp = properties.getProperty("sms.temp.reset");
			this.callingTemp = properties.getProperty("sms.temp.calling");
		} catch (Exception e) {
			logger.debug("配置文件获取异常！" + e.toString());
		}
	}

	/**
	 * 发送短信验证码
	 *
	 * @param mobile
	 * @param code
	 */
	public void sendSMS(String mobile, String code, String type) {
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setSmsType("normal");
		req.setRecNum(mobile);
		if ("reg".equals(type)) {
			req.setSmsParamString("{\"code\":\"" + code + "\",\"product\":\"云宝科技\"}");
			req.setSmsFreeSignName(properties.getProperty("sms.sign.name.reg"));
			req.setSmsTemplateCode(regTemp);
		}
		if ("reset".equals(type)) {
			req.setSmsParamString("{\"code\":\"" + code + "\",\"product\":\"天年宝\"}");
			req.setSmsFreeSignName(properties.getProperty("sms.sign.name.reset"));
			req.setSmsTemplateCode(resetTemp);
		}
		if ("calling".equals(type)) {
			req.setSmsParamString("{\"customer\":\"" + code + "\"}");
			req.setSmsFreeSignName(properties.getProperty("sms.sign.name.temp"));
			req.setSmsTemplateCode(callingTemp);
		}
		try {
			logger.debug("send sms number start" + mobile + " " + code);
			AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
			logger.debug("send sms number end" + mobile + " " + code);
			logger.debug("短信发送成功 code：" + mobile +" "+ code+ " "+rsp.getErrorCode()+" "+rsp.getBody());
		} catch (Exception e) {
			logger.debug("发送短信验证码失败！");
		}
	}

	public static void main(String[] args) {
		SMSUtil smsUtil = new SMSUtil();
		smsUtil.sendSMS("18518661359", "112332", "reset");
	}

	/**
	 * 重置密码
	 *
	 * @author ytche
	 * @param mobile
	 * @return
	 * @date 2017年9月7日 上午11:47:28
	 */
	public Integer sendResetCode(String mobile) {
		// 设置生成验证码的上下阈值
		Integer min = 100000;
		Integer max = 999999;
		Integer code = new Random().nextInt(max) % (max - min + 1) + min;
		this.sendSMS(mobile, code.toString(), CommonConstants.SMS_TYPE_RESET);
		return code;
	}
}
